package huzh.foundation.pool.threadpool;

/**
 *<p>title:DefaultConsumer</p>
 *<p>description:</p>
 * @since 2014-6-7
 * @author huzh
 * @version 1.0
 */
class DefaultConsumer implements Consumer{
    /**defaultConsumer*/
    private static DefaultConsumer defaultConsumer = null;

    /**
     * @return
     */
    public static DefaultConsumer getInstance(){
        if (defaultConsumer == null) {
            defaultConsumer = new DefaultConsumer();
        }
        return defaultConsumer;
    }

    /**
     * @param obj
     */
    public void doWork(Object obj) {
        JobClass jc = null;
        try {
            jc = (JobClass)obj;
            Object job = jc.getJob_obj();
            Object para = jc.getPara_obj();
            if ((job instanceof AbstractTask)) {
                try {
                    ((AbstractTask)job).setDone(false);
                    ((AbstractTask)job).run(para);
                }finally {
                    ((AbstractTask)job).setDone(true);
                }
            } else if ((job instanceof ParaRunnable)) {
                ((ParaRunnable)job).run(para);
            } else {
                ((Runnable)job).run();
            }
        } catch (Exception ex) {
            System.out.println("Exception at dowork: " + ex);
            ex.printStackTrace();
        } finally {
            jc = null;
        }
    }
  }
